package mpweixin

import (
	"context"

	"lianzi_gos/internal/mods/mpweixin/api"
	"lianzi_gos/internal/mods/mpweixin/schema"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type MPWEIXIN struct {
	DB            *gorm.DB
	MemberAPI     *api.Member
	GoodsAPI      *api.Goods
	BannerAPI     *api.Banner
	StaffAPI      *api.Staff
	BusinessAPI   *api.Business
	BrandAPI      *api.Brand
	GoodsAttrAPI  *api.GoodsAttr
	SyscfgAPI     *api.Syscfg
	ProcureAPI    *api.Procure
	CollectAPI    *api.Collect
	RecordAPI     *api.Record
	CategoryAPI   *api.Category
	ConnectionAPI *api.Connection
	BuyerAPI      *api.Buyer
}

func (a *MPWEIXIN) AutoMigrate(ctx context.Context) error {
	return a.DB.AutoMigrate(new(schema.Member), new(schema.Goods), new(schema.Banner), new(schema.Staff), new(schema.Business), new(schema.Brand), new(schema.GoodsAttr), new(schema.Syscfg), new(schema.Procure), new(schema.Collect), new(schema.Record), new(schema.Category), new(schema.Connection), new(schema.Buyer))
}

func (a *MPWEIXIN) Init(ctx context.Context) error {
	if err := a.AutoMigrate(ctx); err != nil {
		return err
	}
	return nil
}

func (a *MPWEIXIN) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup) error {
	members := v1.Group("members")
	{
		members.GET("", a.MemberAPI.Query)
		members.GET("get-user-info", a.MemberAPI.GetUserInfo)
		members.PUT("phone", a.MemberAPI.UpdatePhone)
		members.PUT("user-info", a.MemberAPI.UpdateNicknameAvatar)
		members.POST("", a.MemberAPI.Create)
		members.PUT(":id", a.MemberAPI.Update)
		members.DELETE(":id", a.MemberAPI.Delete)
	}

	wx_mini_login := v1.Group("wx_mini_login")
	{
		wx_mini_login.POST("", a.MemberAPI.Login)
	}
	wx_mini_phone := v1.Group("wx_mini_phone")
	{
		wx_mini_phone.POST("", a.MemberAPI.GetMiniPhone)
	}
	goods := v1.Group("goods")
	{
		goods.GET("", a.GoodsAPI.Query)
		goods.GET(":id", a.GoodsAPI.Get)
		goods.GET(":id/detail", a.GoodsAPI.GetDetail)
		goods.POST("", a.GoodsAPI.Create)
		goods.PUT(":id", a.GoodsAPI.Update)
		goods.PUT(":id/goods-success", a.GoodsAPI.UpdateSuccess)
		goods.PUT(":id/goods-overrule", a.GoodsAPI.UpdateOverrule)
		goods.PUT(":id/goods-approved", a.GoodsAPI.UpdateApproved)
		goods.PUT(":id/goods-sale", a.GoodsAPI.UpdateSale)
		goods.PUT(":id/goods-selection", a.GoodsAPI.UpdateSelection)
		goods.GET(":id/goods-belonging", a.GoodsAPI.GetGoodsBelongingDetail)
		goods.PUT(":id/goods-recommend", a.GoodsAPI.UpdateRecommend)
		goods.PUT(":id/goods-publish", a.GoodsAPI.UpdatePublish)
		goods.PUT(":id/goods-price", a.GoodsAPI.UpdatePrice)
		goods.PUT(":id/goods-status", a.GoodsAPI.UpdateStatus)
		goods.PUT(":id/goods-two", a.GoodsAPI.UpdateTwoSuccess)
		goods.DELETE(":id", a.GoodsAPI.Delete)
	}

	goods_list := v1.Group("goods-list")
	{
		goods_list.GET("", a.GoodsAPI.Query)
		goods_list.GET(":id", a.GoodsAPI.Get)
		goods_list.GET(":id/detail", a.GoodsAPI.GetDetail)
	}

	banner := v1.Group("banners")
	{
		banner.GET("", a.BannerAPI.Query)
		banner.GET(":id", a.BannerAPI.Get)
		banner.POST("", a.BannerAPI.Create)
		banner.PUT(":id", a.BannerAPI.Update)
		banner.DELETE(":id", a.BannerAPI.Delete)
	}
	staff := v1.Group("staffs")
	{
		staff.GET("", a.StaffAPI.Query)
		staff.GET(":id", a.StaffAPI.Get)
		staff.POST("", a.StaffAPI.Create)
		staff.PUT(":id", a.StaffAPI.Update)
		staff.DELETE(":id", a.StaffAPI.Delete)
	}
	business := v1.Group("businesses")
	{
		business.GET("", a.BusinessAPI.Query)
		business.GET(":id", a.BusinessAPI.Get)
		business.POST("", a.BusinessAPI.Create)
		business.PUT(":id", a.BusinessAPI.Update)
		business.DELETE(":id", a.BusinessAPI.Delete)
	}
	brand := v1.Group("brands")
	{
		brand.GET("", a.BrandAPI.Query)
		brand.GET(":id", a.BrandAPI.Get)
		brand.POST("", a.BrandAPI.Create)
		brand.PUT(":id", a.BrandAPI.Update)
		brand.DELETE(":id", a.BrandAPI.Delete)
	}
	goodsAttr := v1.Group("goods-attrs")
	{
		goodsAttr.GET("", a.GoodsAttrAPI.Query)
		goodsAttr.GET(":id", a.GoodsAttrAPI.Get)
		goodsAttr.POST("", a.GoodsAttrAPI.Create)
		goodsAttr.PUT(":id", a.GoodsAttrAPI.Update)
		goodsAttr.DELETE(":id", a.GoodsAttrAPI.Delete)
		goodsAttr.PUT(":id/attr-status", a.GoodsAttrAPI.UpdateAttrStatus)
		goodsAttr.PUT(":id/attr-price", a.GoodsAttrAPI.UpdateAttrPrice)
	}
	syscfg := v1.Group("syscfgs")
	{
		syscfg.GET("", a.SyscfgAPI.Query)
		syscfg.GET(":id", a.SyscfgAPI.Get)
		syscfg.POST("", a.SyscfgAPI.Create)
		syscfg.PUT(":id", a.SyscfgAPI.Update)
		syscfg.DELETE(":id", a.SyscfgAPI.Delete)
		syscfg.GET("qiniu-token", a.SyscfgAPI.GetQiniuToken)
	}
	procure := v1.Group("procures")
	{
		procure.GET("", a.ProcureAPI.Query)
		procure.GET(":id", a.ProcureAPI.Get)
		procure.POST("", a.ProcureAPI.Create)
		procure.PUT(":id", a.ProcureAPI.Update)
		procure.PUT(":id/contact", a.ProcureAPI.UpdateContact)
		procure.PUT(":id/contact-type", a.ProcureAPI.UpdateContactType)
		procure.DELETE(":id", a.ProcureAPI.Delete)
	}
	collect := v1.Group("collects")
	{
		collect.GET("", a.CollectAPI.Query)
		collect.GET(":id", a.CollectAPI.Get)
		collect.POST("", a.CollectAPI.Create)
		collect.PUT(":id", a.CollectAPI.Update)
		collect.DELETE(":id", a.CollectAPI.Delete)
	}
	record := v1.Group("records")
	{
		record.GET("", a.RecordAPI.Query)
		record.GET(":id", a.RecordAPI.Get)
		record.POST("", a.RecordAPI.Create)
		record.PUT(":id", a.RecordAPI.Update)
		record.DELETE(":id", a.RecordAPI.Delete)
	}
	category := v1.Group("categorys")
	{
		category.GET("", a.CategoryAPI.Query)
		category.GET(":id", a.CategoryAPI.Get)
		category.POST("", a.CategoryAPI.Create)
		category.PUT(":id", a.CategoryAPI.Update)
		category.DELETE(":id", a.CategoryAPI.Delete)
	}
	connection := v1.Group("connections")
	{
		connection.GET("", a.ConnectionAPI.Query)
		connection.GET(":id", a.ConnectionAPI.Get)
		connection.POST("", a.ConnectionAPI.Create)
		connection.PUT(":id", a.ConnectionAPI.Update)
		connection.DELETE(":id", a.ConnectionAPI.Delete)
	}
	buyer := v1.Group("buyers")
	{
		buyer.GET("", a.BuyerAPI.Query)
		buyer.GET(":id", a.BuyerAPI.Get)
		buyer.POST("", a.BuyerAPI.Create)
		buyer.PUT(":id", a.BuyerAPI.Update)
		buyer.DELETE(":id", a.BuyerAPI.Delete)
	}

	return nil
}
func (a *MPWEIXIN) Release(ctx context.Context) error {
	return nil
}
